package com.alks.function.service.impl.biocontrol.deptwork;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.ArrayUtils;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcDeptPersons;
import com.alks.entity.data.entity.PcLotHead;
import com.alks.entity.data.entity.PcLotProdPlan;
import com.alks.entity.data.entity.PcProcessIePlan;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.biocontrol.deptwork.PcLotProdPlanDto;
import com.alks.function.data.request.biocontrol.deptwork.ProdPlanRequest;
import com.alks.function.data.request.biocontrol.deptwork.ProdPlanUpRequest;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcDeptPersonsMapper;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcLotProdPlanMapper;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcProcessIePlanMapper;
import com.alks.function.mapper.chengKong.PcLotHeadMapper;
import com.alks.function.service.biocontrol.deptwork.IProdPlanService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.alks.entity.data.enums.ResultCodeEnum.INPUT_ERROR;

@Service
@Slf4j
@RequiredArgsConstructor
public class ProdPlanServiceImpl extends ServiceImpl<BC_DW_PcLotProdPlanMapper, PcLotProdPlan> implements IProdPlanService {
    private final BC_DW_PcLotProdPlanMapper prodPlanMapper;
    private final BC_DW_PcProcessIePlanMapper iePlanMapper;
    private final BC_DW_PcDeptPersonsMapper deptPersonsMapper;
    private final PcLotHeadMapper lotHeadMapper;

    @Override
    @AutoPageAop
    public ResponseInfo<Map<String, Object>> getProdPlanQuery(ProdPlanRequest request) {
        /*入参校验*/
        if (BeanUtil.allIsEmpty(request.getProcess1())) {
            request.setShortFlag(null);
        }
        if (BeanUtil.allIsEmpty(request.getProcess2())) {
            request.setLateType(null);
        }
        /*获取数据*/
        List<PcLotProdPlanDto> dtos = prodPlanMapper.getProdPlanQuery(request, UserIdThread.get().getCompanyId());
        /*封装返回*/
        Map<String, Object> map = new HashMap<>();
        map.put("dtos", dtos);
        if (request.getPageSize() != null && request.getPageSize() != -1) {
            Page<PcLotProdPlanDto> page = (Page<PcLotProdPlanDto>) dtos;
            map.put("total", page.getTotal());
        }
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo prodPlanUpdate(ProdPlanUpRequest request) {
        /*更新数据*/
        lambdaUpdate().eq(PcLotProdPlan::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(PcLotProdPlan::getLotNo, request.getLotNo())
                .set(PcLotProdPlan::getCutDept, request.getCutDept())
                .set(PcLotProdPlan::getCutPlanNew, request.getCutPlanNew())
                .set(PcLotProdPlan::getCutOutQty, request.getCutOutQty())
                .set(PcLotProdPlan::getHfPlanNew, request.getHfPlanNew())
                .set(PcLotProdPlan::getHfOutQty, request.getHfOutQty())
                .set(PcLotProdPlan::getMatchPlanNew, request.getMatchPlanNew())
                .set(PcLotProdPlan::getMatchOutQty, request.getMatchOutQty())
                .set(PcLotProdPlan::getStitchPlanNew, request.getStitchPlanNew())
                .set(PcLotProdPlan::getStitchOutQty, request.getStitchOutQty())
                .set(PcLotProdPlan::getLastPlanNew, request.getLastPlanNew())
                .set(PcLotProdPlan::getLastOutQty, request.getLastOutQty())
                .update();
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getDeptPersonQuery() {
        /*获取数据*/
        LambdaQueryWrapper<PcDeptPersons> wrapper = new LambdaQueryWrapper<PcDeptPersons>()
                .eq(PcDeptPersons::getCompanyId, UserIdThread.get().getCompanyId());
        List<PcDeptPersons> dtos = deptPersonsMapper.selectList(wrapper);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo deptPersonUpdate(BigDecimal lastPersons, BigDecimal stitchPersons, BigDecimal cutPersons) {
        /*创建实体类*/
        PcDeptPersons persons = new PcDeptPersons();
        persons.setLastPersons(lastPersons);
        persons.setStitchPersons(stitchPersons);
        persons.setCutPersons(cutPersons);
        persons.setCompanyId(UserIdThread.get().getCompanyId());
        /*更新数据*/
        LambdaUpdateWrapper<PcDeptPersons> wrapper = new LambdaUpdateWrapper<PcDeptPersons>()
                .eq(PcDeptPersons::getCompanyId, UserIdThread.get().getCompanyId());
        deptPersonsMapper.update(persons, wrapper);
        return ResponseInfo.ok();
    }

    //TODO 指令更新待完成
    @Override
    public ResponseInfo prodLotUpdate() {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取缺失数据*/
        List<String> lotNoList = lambdaQuery()
                .select(PcLotProdPlan::getLotNo)
                .eq(PcLotProdPlan::getCompanyId, companyId)
                .groupBy(PcLotProdPlan::getLotNo)
                .list().stream()
                .map(PcLotProdPlan::getLotNo)
                .collect(Collectors.toList());
        LambdaQueryWrapper<PcLotHead> lotWrapper = new LambdaQueryWrapper<PcLotHead>()
                .select(PcLotHead::getCompanyId, PcLotHead::getLotNo, PcLotHead::getCustom,
                        PcLotHead::getStyle, PcLotHead::getColor, PcLotHead::getOrderQty, PcLotHead::getSurplusQty,
                        PcLotHead::getLastNo, PcLotHead::getOrderDate, PcLotHead::getDeliveryDate)
                /*半年内的数据*/
                .ge(PcLotHead::getSysDate,LocalDateTime.now().minusMonths(6))
                .eq(PcLotHead::getCfmFlag,"Y")
                .eq(PcLotHead::getCompanyId, companyId)
                .notIn(PcLotHead::getLotNo, lotNoList);
        List<PcLotHead> lotHeadList = lotHeadMapper.selectList(lotWrapper);
        if (ArrayUtils.isEmpyt(lotHeadList)) {
            return ResponseInfo.ok();
        }
        for (PcLotHead lotHead : lotHeadList) {
            /*写入新数据*/
            PcLotProdPlan prodPlan = new PcLotProdPlan();
            prodPlan.setCompanyId(lotHead.getCompanyId());
            prodPlan.setLotNo(lotHead.getLotNo());
            prodPlan.setCustom(lotHead.getCustom());
            prodPlan.setStyle(lotHead.getStyle());
            prodPlan.setStyle(lotHead.getStyle());
            prodPlan.setColor(lotHead.getColor());
            BigDecimal orederQty = lotHead.getOrderQty() == null ? BigDecimal.ZERO : BigDecimal.valueOf(lotHead.getOrderQty());
            prodPlan.setOrderQty(orederQty);
            prodPlan.setLastNo(lotHead.getLastNo());
            prodPlan.setOrderDate(lotHead.getOrderDate());
            prodPlan.setDeliveryDate(lotHead.getDeliveryDate());
            prodPlanMapper.insert(prodPlan);
        }
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo prodDataUpdate() {

        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo getIePlanQuery() {
        /*获取数据*/
        LambdaQueryWrapper<PcProcessIePlan> wrapper = new LambdaQueryWrapper<PcProcessIePlan>()
                .eq(PcProcessIePlan::getCompanyId, UserIdThread.get().getCompanyId());
        List<PcProcessIePlan> dtos = iePlanMapper.selectList(wrapper);
        return ResponseInfo.ok(dtos);
    }

    @Override
    public ResponseInfo IePlanUpdate(PcProcessIePlan request, String seqNoOld) {
        /*判断序号合法性*/
        if (request.getSeqNo() == null || seqNoFlag(request.getSeqNo()) == null) {
            return ResponseInfo.error("序号不能重复或者为空", INPUT_ERROR.getCode());
        }
        /*补全实体类*/
        request.setCompanyId(UserIdThread.get().getCompanyId());
        /*更新数据*/
        LambdaUpdateWrapper<PcProcessIePlan> wrapper = new LambdaUpdateWrapper<PcProcessIePlan>()
                .eq(PcProcessIePlan::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(PcProcessIePlan::getSeqNo, request.getSeqNo());
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo iePlanAdd(PcProcessIePlan request) {
        /*处理序号为空的情况*/
        if (request.getSeqNo() == null) {
            request.setSeqNo(iePlanMapper.seqNoMaxQuery(UserIdThread.get().getCompanyId()));
        }
        /*处理序号重复的情况*/
        if (seqNoFlag(request.getSeqNo()) == null) {
            return ResponseInfo.error("序号不能重复", INPUT_ERROR.getCode());
        }
        /*写入数据*/
        request.setCompanyId(UserIdThread.get().getCompanyId());
        iePlanMapper.insert(request);
        return ResponseInfo.ok();
    }

    private BigDecimal seqNoFlag(BigDecimal seqNo) {
        LambdaQueryWrapper<PcProcessIePlan> wrapper = new LambdaQueryWrapper<PcProcessIePlan>()
                .eq(PcProcessIePlan::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(PcProcessIePlan::getSeqNo, seqNo);
        if (iePlanMapper.selectCount(wrapper) > 0) {
            return null;
        }
        return seqNo;
    }

    @Override
    public ResponseInfo iePlanDelete(String seqNo) {
        /*删除数据*/
        LambdaQueryWrapper<PcProcessIePlan> wrapper = new LambdaQueryWrapper<PcProcessIePlan>()
                .eq(PcProcessIePlan::getCompanyId, UserIdThread.get().getCompanyId())
                .eq(PcProcessIePlan::getSeqNo, seqNo);
        iePlanMapper.delete(wrapper);
        return ResponseInfo.ok();
    }
}
